TOOL.Category = "NPC Control - Actors"
TOOL.Name = "Animations"
TOOL.Command = nil
TOOL.ConfigName = ""

local NPCOptions = {}
local existing_seq = {}

if CLIENT then
  language.Add( "Tool_npc_actor_anim_name", "Actor Control: Animations" )
  language.Add( "Tool_npc_actor_anim_desc", "Make NPC actor's animate" )
  language.Add( "Tool_npc_actor_anim_0", "Left-Click to make an NPC animate; Right-Click to stop them from animating" )
  language.Add( "cycler_actor", "Actor" )
end

TOOL.ClientConVar["npcanim"] = "Sit Down (On ground)"

local menustatus = 0 //Closed
local selectednpc = nil

//LEFT CLICK HERE
function TOOL:RightClick( trace )
  if (trace.Entity:IsValid() && trace.Entity:IsNPC()) then
    for k, v in pairs(existing_seq) do
      if (v != nil && v.npc == trace.Entity) then
        if (v != nil && v.seq != nil) then v.seq:Remove() end
        v.npc = nil
      end
    end
  end
  return true
end

local function DoAnimation( ply, npc, ani1, ani2 )
  if (SERVER || SinglePlayer()) then
    local npcname = npc:GetName()
    local newname = npc:GetName()
    local seqname = ""
    local seqnum = 0
    
    //If another one for that NPC exists, destroy it to allow for other scripts
    
    for k, v in pairs(existing_seq) do
      if (v != nil) then
        seqnum = seqnum + 1
      end
    end
    seqname = ply:GetName() .. "_scriptseq" .. tostring(seqnum)
    
    local namecount = 0
    for k, v in pairs(ents.FindByName(npcname)) do
      namecount = namecount + 1
    end
    if (namecount > 1 || npcname == "") then
      npc:SetName(ply:GetName() .. "_scriptme")
      newname = ply:GetName() .. "_scriptme"
    end
    //Create the sequence
    local newseq = ents.Create("scripted_sequence")
    newseq:SetKeyValue("m_fMoveTo", "0")
    newseq:SetKeyValue("m_iszEntity", newname)
    newseq:SetKeyValue("m_iszEntry", ani1) //Entry
    newseq:SetKeyValue("m_iszPlay", ani2)
    newseq:SetKeyValue("m_bLoopActionSequence", "1")
    newseq:SetKeyValue("spawnflags", "4288")
    newseq:SetKeyValue("targetname", seqname)
    newseq:SetPos(Vector(0, 0, 0))
    newseq:Spawn()
    newseq:Fire("BeginSequence", "", 0)
    existing_seq[newseq] = { npc = npc, seq = newseq }
    /*if (npcname != newname) then
      npc:SetName(npcname)
    end*/ //This had to go, it was causing the scripted sequence to fail
  end
end


function TOOL:LeftClick( trace )
  if (trace.Entity:IsValid() && trace.Entity:IsNPC() && trace.Entity:GetClass() == "cycler_actor") then
    //Doesn't want to work properly
    /*for k, v in pairs(existing_seq) do
      if (v != nil && v.npc == trace.Entity) then
        if (v.seq != nil) then v.seq:Remove() end
        v.npc = nil
      end
    end*/
    DoAnimation( self:GetOwner(), trace.Entity, NPCOptions.Global.Animations[self:GetClientInfo("npcanim")].start, NPCOptions.Global.Animations[self:GetClientInfo("npcanim")].after )
    return true
  elseif (trace.Entity:GetClass() != "cycler_actor") then
    self:GetOwner():PrintMessage(HUD_PRINTTALK, "That is not an actor")
  end
end

NPCOptions.Base   = { npcclass = "cycler_actor", options = {}, SpawnSpecs = { spawnflags = 25108 } }
NPCOptions.Global = {}
//Global Animations
NPCOptions.Global.Animations = {}
NPCOptions.Global.Animations["Waiting in Line (1)"]   = { start = "", after = "LineIdle01" }
NPCOptions.Global.Animations["Waiting in Line (2)"]   = { start = "", after = "LineIdle02" }
NPCOptions.Global.Animations["Waiting in Line (3)"]   = { start = "", after = "LineIdle03" }
NPCOptions.Global.Animations["Startle from behind"]   = { start = "Startle_behind", after = "idle_subtle" }
NPCOptions.Global.Animations["Photo-blindness"]       = { start = "photo_react_blind", after = "idle_subtle" }
NPCOptions.Global.Animations["Wave"]                  = { start = "wave", after = "idle_subtle" }
NPCOptions.Global.Animations["Wave (Close)"]          = { start = "wave_close", after = "idle_subtle" }
NPCOptions.Global.Animations["Scared"]                = { start = "", after = "scaredidle" }
NPCOptions.Global.Animations["Scared for Cover"]      = { start = "fear_reaction", after = "fear_reaction_idle" }
NPCOptions.Global.Animations["Cower"]                 = { start = "cower", after = "cower_idle" }
NPCOptions.Global.Animations["Swing Fist"]            = { start = "swing", after = "idle_subtle" }
NPCOptions.Global.Animations["Sit Down (On ground)"]  = { start = "idle_to_sit_ground", after = "sit_ground" }
NPCOptions.Global.Animations["Sit Down (On chair)"]   = { start = "idle_to_sit_chair", after = "sit_chair" }
NPCOptions.Global.Animations["(Vortigaunt) Ready for action"] = { start = "ToAction", after = "ActionIdle" }
NPCOptions.Global.Animations["(Vortigaunt) Cook"]             = { start = "", after = "butcher" }
NPCOptions.Global.Animations["(Vortigaunt) Cook 2"]           = { start = "", after = "soupstir" }
NPCOptions.Global.Animations["(Vortigaunt) Calm down"]        = { start = "FromAction", after = "Idle01" }
NPCOptions.Global.Animations["(Vortigaunt) Chess - Sitting"]  = { start = "", after = "chess_wait" }
NPCOptions.Global.Animations["(Combine) Bugbait reaction"]    = { start = "", after = "bugbait_hit" }
NPCOptions.Global.Animations["(Alyx) Flip Left"]              = { start = "FlipLeft", after = "idle_subtle" }
NPCOptions.Global.Animations["(Alyx) Flip Right"]             = { start = "FlipRight", after = "idle_subtle" }
NPCOptions.Global.Animations["(Alyx) Keypad"]                 = { start = "ButtonPush", after = "idle_subtle" }
//Doesn't work
//NPCOptions.Global.Animations["(Alyx) Idle Hurt"]              = { start = "", after = "injured_idle" }
NPCOptions.Global.Animations["(G-Man) Tie Fidget"]            = { start = "tiefidget", after = "" }

function TOOL.BuildCPanel( panel )
  local data = {}
  data.Label = "Animations"
  data.Options = {}
  for k, v in pairs(NPCOptions.Global.Animations) do
    data.Options[tostring(k)] = {npc_actor_anim_npcanim = tostring(k)}
  end
  data.Height = 240
  panel:AddControl("ListBox", data)
  panel:AddControl("Label", {Text = "Some actor's can't do all the animations in the list."})
  panel:AddControl("Label", {Text = "Please note that this is a beta tool, and may be buggy"})
end
